package cn.easybuy.dao.impl;

import cn.easybuy.dao.ProductCategoryDao;
import cn.easybuy.dao.ProductDao;
import cn.easybuy.entity.Product;
import cn.easybuy.entity.ProductCategory;
import cn.easybuy.utils.Utils;

import java.sql.Connection;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class ProductCategoryDaoImpl implements ProductCategoryDao {
    /**
     * 查询所有商品分类信息
     * @return
     */
    @Override
    public List<ProductCategory> getAllProductCategory() {
        String sql = "SELECT * FROM easybuy_product_category";
        Connection connection = null;
        ResultSet rs = null;
        ProductCategory productCategory =null;
        List<ProductCategory> list = new ArrayList<ProductCategory>();
        try{
            connection =  Utils.getConnection();
            rs =  Utils.select(sql,connection);
            while(rs.next()){
                productCategory =new ProductCategory();
                productCategory.setId(rs.getLong("id"));
                productCategory.setName(rs.getString("name"));
                productCategory.setParentId(rs.getLong("parentId"));
                productCategory.setType(rs.getLong("type"));
                productCategory.setIconClass(rs.getString("iconClass"));
                list.add(productCategory);
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally {
            Utils.closeAll(connection,null,rs);
        }
        return list;
    }
    /**
     * 通过商品分类id查询商品分类信息
     * @return
     */
    @Override
    public ProductCategory getProductCategoryById(Long id) {
        String sql = "SELECT * FROM easybuy_product_category where id=?";
        Connection connection = null;
        ResultSet rs = null;
        ProductCategory productCategory =null;
        try{
            connection =  Utils.getConnection();
            rs =  Utils.select(sql,connection);
            while(rs.next()){
                productCategory =new ProductCategory();
                productCategory.setId(rs.getLong("id"));
                productCategory.setName(rs.getString("name"));
                productCategory.setParentId(rs.getLong("parentId"));
                productCategory.setType(rs.getLong("type"));
                productCategory.setIconClass(rs.getString("iconClass"));
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally {
            Utils.closeAll(connection,null,rs);
        }
        return productCategory;
    }
    /**
     * 通过商品分类name查询商品分类信息
     * @return
     */
    @Override
    public ProductCategory getProductCategoryByName(String name) {
        String sql = "SELECT * FROM easybuy_product_category where name=?";
        Connection connection = null;
        ResultSet rs = null;
        ProductCategory productCategory =null;
        try{
            connection =  Utils.getConnection();
            rs =  Utils.select(sql,connection);
            while(rs.next()){
                productCategory =new ProductCategory();
                productCategory.setId(rs.getLong("id"));
                productCategory.setName(rs.getString("name"));
                productCategory.setParentId(rs.getLong("parentId"));
                productCategory.setType(rs.getLong("type"));
                productCategory.setIconClass(rs.getString("iconClass"));
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally {
            Utils.closeAll(connection,null,rs);
        }
        return productCategory;
    }
    /**
     * 通过父类目录id查询商品分类信息
     * @return
     */
    @Override
    public ProductCategory getProductCategoryByParentId(Long parentId) {
        String sql = "SELECT * FROM easybuy_product_category where parentId=?";
        Connection connection = null;
        ResultSet rs = null;
        ProductCategory productCategory =null;
        try{
            connection =  Utils.getConnection();
            rs =  Utils.select(sql,connection);
            while(rs.next()){
                productCategory =new ProductCategory();
                productCategory.setId(rs.getLong("id"));
                productCategory.setName(rs.getString("name"));
                productCategory.setParentId(rs.getLong("parentId"));
                productCategory.setType(rs.getLong("type"));
                productCategory.setIconClass(rs.getString("iconClass"));
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally {
            Utils.closeAll(connection,null,rs);
        }
        return productCategory;
    }
    /**
     * 添加商品分类信息
     * @param productCategory
     * @return
     */
    @Override
    public int addProductCategory(ProductCategory productCategory) {
        String sql ="INSERT INTO easybuy_product_category(name,parentId,type,iconClass) VALUES (?,?,?,?)";
        return Utils.execute(sql,productCategory.getName(),productCategory.getParentId(),productCategory.getType(),productCategory.getIconClass());
    }

    /**
     * 修改商品信息
     * @param productCategory
     * @return
     */
    @Override
    public int updateProductCategory(ProductCategory productCategory) {
        String sql ="update easybuy_product_category set name=?,parentId=?,type=?,iconClass=？ where id=?";
        return Utils.execute(sql,productCategory.getId());

    }
    /**
     * 通过商品分类id删除商品分类信息
     * @param id
     * @return
     */
    @Override
    public int deleteProductCategory(Long id) {
        String sql ="delete from easybuy_product_category where id=?";
        return Utils.execute(sql,id);
    }
    /**
     * 通过商品分类name删除商品分类信息
     * @param name
     * @return
     */
    @Override
    public int deleteProductCategory(String name) {
        String sql ="delete from easybuy_product_category where id=?";
        return Utils.execute(sql,name);
    }
}
